package com.ludi.basics.service.impl;

import com.ludi.basics.pojo.entity.User;
import com.ludi.basics.pojo.qo.UserLoginQO;
import com.ludi.basics.service.UserService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ludi
 * @date:2020/8/21 14:38
 * @description:用户业务
 */
@Service
public class UserServiceImpl implements UserService {
    @Override
    public User login(UserLoginQO userLoginQO) {
        //自创数据库用户数据、验证码
        User dbUser = new User("小明","123456");
        //自创验证码
        String code = "aaaa";
        //判断验证码
        if(!code.equalsIgnoreCase(userLoginQO.getValiCode())){
            throw new RuntimeException("验证码错误");
        }
        //判断用户名
        if(!dbUser.getUserName().equals(userLoginQO.getUserName())){
            throw new RuntimeException("用户名不正确");
        }
        //判断密码
        if(!dbUser.getPassword().equals(userLoginQO.getPassword())){
            throw new RuntimeException("密码错误");
        }
        return dbUser;
    }

    @Override
    public List<User> show() {
        //自创用户列表
        List<User> list = new ArrayList<>();
        list.add(new User("豪哥","123456"));
        list.add(new User("小花","555555"));
        list.add(new User("小明","123456"));
        return list;
    }

    @Override
    public User register(String userName, String password) {
        //自创用户列表
        List<User> list = new ArrayList<>();
        list.add(new User("豪哥","123456"));
        list.add(new User("小花","555555"));
        list.add(new User("小明","123456"));

        for (User user : list) {
            if(user.getUserName().equals(userName)){
                throw new RuntimeException("用户已存在");
            }
        }
        User newUser = new User(userName, password);
        list.add(newUser);
        return newUser;
    }
}
